package kuaidi.utils;


import kuaidi.dao.KInfoInsert;
import kuaidi.pojo.kBean;
import org.apache.ibatis.session.SqlSession;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

public class readexcel {
    public static void main(String[] args) {

        SqlSession sqlSession = umybatis.getSqlSession();
        KInfoInsert mapper = sqlSession.getMapper(KInfoInsert.class);

        HashMap<Object, Object> objectObjectHashMap = new HashMap<Object, Object>();
    objectObjectHashMap.put("name","吴弈洁");
        List<kBean> kuaidiInfo = mapper.getKuaidiInfo(objectObjectHashMap);
        for (kBean kBean:kuaidiInfo
             ) {
            System.out.println(kBean.toString());
     }

    }
    public static List<kBean> csv1() {
//        LinkedList<HashMap<Object, Object>> hashMaps = new LinkedList<HashMap<Object, Object>>();
        LinkedList<kBean> list = new LinkedList<kBean>();
        //excel文件路径

        String excelPath = "C:\\Users\\charzhamn\\Desktop\\1.xls";
        HashMap<Object, Object> objectObjectHashMap = new HashMap<Object, Object>();

        try {
            //String encoding = "GBK";
            File excel = new File(excelPath);
            if (excel.isFile() && excel.exists()) {   //判断文件是否存在

                String[] split = excel.getName().split("\\.");  //.是特殊字符，需要转义！！！！！
                Workbook wb = null;
                //根据文件后缀（xls/xlsx）进行判断
                if ("xls".equals(split[1])) {
                    FileInputStream fis = new FileInputStream(excel);   //文件流对象
                    wb = new HSSFWorkbook(fis);
                } else if ("xlsx".equals(split[1])) {
                    wb = new XSSFWorkbook(excel);
                } else {
                    System.out.println("文件类型错误!");

                }

                //开始解析
                //读取sheet 0
                Sheet sheet = wb.getSheetAt(0);

                int firstRowIndex = sheet.getFirstRowNum() + 1;   //第一行是列名，所以不读
                int lastRowIndex = sheet.getLastRowNum();
                System.out.println("firstRowIndex: " + firstRowIndex);
                System.out.println("lastRowIndex: " + lastRowIndex);
                String banji="";

                for (int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {   //遍历行
//                    System.out.println("rIndex: " + rIndex);
                    kBean teacherBean = new kBean();
                    Row row = sheet.getRow(rIndex);

                    if (row != null) {
                        int firstCellIndex = row.getFirstCellNum();
                        int lastCellIndex = row.getLastCellNum();
                        Cell cell1 = row.getCell(0);
                            kBean k=new kBean();
                         String snum="";
                        for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) {   //遍历列

                            Cell cell = row.getCell(cIndex);
                            CellType cellType = cell.getCellType();
                            if (cIndex==0){
                                k.setYear(cell.toString());
                                                            }
                            if (cIndex==1){
                                k.setName(cell.toString());
                            }
                            if (cIndex==2){
                                k.setGender(cell.toString());
                            }
                            if (cIndex==3){
                                if (cellType == CellType.NUMERIC )
                                {
                                    DecimalFormat df = new DecimalFormat("0");
                                    snum = df.format(cell.getNumericCellValue());

                                    k.setPhone(snum);
                                }
                                if (cellType == CellType.STRING)

                                    k.setPhone(cell.toString());
                            }
                            if (cIndex==4){
                                k.setSclass(cell.toString());
                            }
                            if (cIndex==5){
                                k.setMoniter(cell.toString());
                            }
                            if (cIndex==6){
                                if (cellType == CellType.NUMERIC )
                                {
                                    DecimalFormat df = new DecimalFormat("0");
                                    banji = df.format(cell.getNumericCellValue());

                                    k.setMoniterPhone(banji);
                                }
                                if (cellType == CellType.STRING)

                                    k.setMoniterPhone(cell.toString());
                            }
                            if (cIndex==7){
                                k.setClassrome(cell.toString());
                            }


                        }

                      list.add(k);
//                        System.out.println();
                    }
                }
            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


}
